package com.fencing_squad.homo.list;

import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.fencing_squad.homo.ResourceTable;
import com.fencing_squad.homo.util.CustomComponent;
import com.fencing_squad.homo.util.Data;
import com.fencing_squad.homo.util.ImageLoader;
import com.fencing_squad.homo.util.Network;
import com.fencing_squad.homo.util.function.FunctionVoid;
import de.hdodenhof.circleimageview.CircleImageView;
import ohos.agp.components.*;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;

public class CommentProvider extends BaseItemProvider {
    private List<Comment> list;
    private Context context;
    private static final int BEFORE_COUNT = 1;
    private PostUser postUser = null;
    private CommonDialog dialog = null;
    private Component bigest;
    private FunctionVoid func;

    public CommentProvider(Context context, FunctionVoid func) {
        list = new ArrayList<>();
        this.context = context;
        this.postUser = new PostUser();
        this.func = func;
    }

    public CommentProvider(Context context, PostUser postUser, FunctionVoid func) {
        list = new ArrayList<>();
        this.context = context;
        this.postUser = postUser;
        this.func = func;
    }

    public class Holder {
        private CircleImageView avatar;
        private Text nickname;
        private Text content;

        public Holder(Component component) {
            avatar = (CircleImageView) component.findComponentById(ResourceTable.Id_icon);
            nickname = (Text) component.findComponentById(ResourceTable.Id_nickname);
            content = (Text) component.findComponentById(ResourceTable.Id_content);
        }
    }

    public class HolderPost {
        CircleImageView avatar;
        Text nickname;
        Text date;
        Text content;
        Text likes;
        Text comment;
        Image image;
        Component report;

        public HolderPost(Component component) {
            nickname = (Text) component.findComponentById(ResourceTable.Id_nickname);
            date = (Text) component.findComponentById(ResourceTable.Id_date);
            content = (Text) component.findComponentById(ResourceTable.Id_content);
            likes = (Text) component.findComponentById(ResourceTable.Id_likes);
            comment = (Text) component.findComponentById(ResourceTable.Id_comment);
            image = (Image) component.findComponentById(ResourceTable.Id_image);
            avatar = (CircleImageView) component.findComponentById(ResourceTable.Id_icon);
            report = component.findComponentById(ResourceTable.Id_report);
        }
    }

    public void append(Comment comment) {
        if(list != null) {
            list.add(comment);
            notifyDataChanged();
        }
    }

    public void append(List<Comment> comments) {
        if(list != null) {
            list.addAll(comments);
            notifyDataChanged();
        }
    }

    public void clear() {
        if (list != null) {
            list.clear();
            notifyDataChanged();
        }
    }

    public void setPostUser(PostUser postUser) {
        this.postUser = postUser;
        notifyDataChanged();
    }

    @Override
    public int getCount() {
        return list == null ? BEFORE_COUNT : list.size() + BEFORE_COUNT;
    }

    @Override
    public Object getItem(int i) {
        if (i == 0) {
            return postUser;
        }
        else if (list != null && i >= BEFORE_COUNT && i < list.size() + BEFORE_COUNT){
            return list.get(i - BEFORE_COUNT);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    public void sendReport(){
        JSONObject jsonObject = new JSONObject();
        System.out.println(postUser.getPostMessageId());
        jsonObject.put("post_message_id", postUser.getPostMessageId());

        Network.postJson("/post/report_post_message", jsonObject.toJSONString(), this::resolve, this::reject);
    }

    private void resolve(JSONObject res) {
        CustomComponent.getToastDialog(context, "举报成功").show();
    }

    private void reject(int code, String message) {
        if(code == 404) {
            dialog = CustomComponent.getDialog(context, message, () -> {
                if(dialog != null) {
                    dialog.hide();
                }
            }, () -> {
                if(dialog != null) {
                    dialog.hide();
                }
            });
            dialog.show();
        }
        else {
            System.out.println(message);
        }
    }

    private void like(Component component){
        JSONObject jsonObject = new JSONObject();
        System.out.println(postUser.getPostMessageId());
        jsonObject.put("post_message_id", postUser.getPostMessageId());

        Network.postJson("/post/like", jsonObject.toJSONString(), this::resolveLike, this::reject);
    }

    private void resolveLike(JSONObject res) {
        clear();
        CustomComponent.getToastDialog(context, "点赞成功").show();
        func.func();
    }

    @Override
    public Component getComponent(int position, Component convertComponent, ComponentContainer componentContainer) {
        if (position == 0) {
            if (postUser == null) {
                return null;
            }
            final Component cpt;
            cpt = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_single_post, null, false);
            HolderPost holder = new HolderPost(cpt);
            cpt.setTag(holder);
            holder.likes.setText(postUser.getLikes());
            holder.nickname.setText(postUser.getNickname());
            holder.comment.setText(postUser.getComment());
            holder.content.setText(postUser.getContent());
            holder.date.setText(postUser.getDate());
            Glide.with(context).load(postUser.getAvatar()).listener(ImageLoader.fail(holder.avatar, ResourceTable.Media_icon)).into(holder.avatar);
            if (!postUser.getImage().equals("")) {
                holder.image.setVisibility(Component.VISIBLE);
                Glide.with(context).load(postUser.getImage()).listener(ImageLoader.fail(holder.image, ResourceTable.Media_icon)).into(holder.image);
            }
            holder.report.setClickedListener(component -> {
                sendReport();
            });
            bigest = cpt.findComponentById(ResourceTable.Id_bigestPost);
            System.out.println(bigest);
            bigest.setDoubleClickedListener(this::like);

            return cpt;
        }
        else if (position >= BEFORE_COUNT && position < list.size() + BEFORE_COUNT) {
            final Component cpt;
            Comment item = list.get(position - BEFORE_COUNT);
            Holder holder;
            if (convertComponent == null || !(convertComponent.getTag() instanceof Holder)) {
                cpt = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_comment, null, false);
                holder = new Holder(cpt);
                cpt.setTag(holder);
            } else {
                cpt = convertComponent;
                holder = (Holder) cpt.getTag();
            }
            holder.nickname.setText(item.getNickname());
            holder.content.setText(item.getContent());
            Glide.with(context).load(item.getAvatar()).listener(ImageLoader.fail(holder.avatar, ResourceTable.Media_icon)).into(holder.avatar);
            return cpt;
        }
        return null;
    }
}
